import { observable, autorun, makeObservable, computed, action, flow, makeAutoObservable } from 'mobx';

class Doubler {
  value
  PI = 3.14
  constructor(value) {
    /*  makeObservable(this, {
       value: observable,
       double: computed,
       add: action.bound,
       fetch: flow.bound
     }) */
    makeAutoObservable(this, { PI: false }, { autoBind: true });
    this.value = value;
  }
  get double() {
    return this.value * 2;
  }
  //action有很多好处 1.action的内部会使用事务的机制 2.只在action修改状态，好排查错误
  add() {
    this.value++;
    this.value++;
  }
  *fetch() {//redux-saga
    let amount = yield new Promise((resolve) => setTimeout(() => resolve(5), 1000));
    this.value += amount;
  }
}
let doubler = new Doubler(1);

autorun(() => {
  console.log(doubler.value);
});
//doubler.value++;

const fetch = doubler.fetch;
fetch();
